package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import com.xiaoleilu.hutool.lang.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.TaskLogService;
import com.symone.webcms.model.TaskLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : TaskLog 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-04-04
 */
@RestController
@Api(value="api/taskLog", description="TaskLog 管理")
@RequestMapping("api/taskLog")
public class TaskLogController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaskLogController.class);

    @Autowired
    public TaskLogService taskLogService;

    /**
     * @description : 获取TaskLog分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("chart:taskLog:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取TaskLog分页列表", notes="获取TaskLog分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tid",value="任务ID"),
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Long tid,Integer pageSize,Integer pageNum) {
        Wrapper<TaskLog> ew = new EntityWrapper<TaskLog>();
        ew.where("1=1");
        if(Validator.isNotEmpty(tid) && tid > 0) {
            ew.and().eq("task_id",tid);
        }
        ew.orderBy("create_time",false);
        //logger.debug("SQL:"+ew.originalSql());

        if(pageNum == null){
            pageNum = 1;
        }

        if(pageSize == null){
            pageSize = 10;
        }

        Page<TaskLog> page = new Page<TaskLog>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = taskLogService.findPage(page, ew);

        return ResultMessage.success("获取TaskLog的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有TaskLog记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
     */
    @RequiresPermissions("chart:taskLog:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有TaskLog信息", notes="获取所有TaskLog信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有TaskLog信息",taskLogService.findAll());
    }

    /**
    * @description : 清除TaskLog记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-04-04
    */
    @RequiresPermissions("chart:taskLog:index")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除TaskLog记录", notes="清除已被逻辑删除的TaskLog记录")
    public ResultMessage clear(){
        int num = taskLogService.deleteAll();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有TaskLog记录被清除！");
        }
    }



}
